<?php

use console\db\channels\TransferTableMigration;
use yii\db\Query;

/**
 * 球迷学院成就迁移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300060_000001_t_college_achieve extends TransferTableMigration {

    /**
     * @inheritdoc
     */
    public $sourceDb = 'olddb';

    /**
     * @inheritdoc
     */
    public function validateTransfer(&$data) {
        if (!parent::validateTransfer($data)) {
            return false;
        }
        $condition = str_replace([
            '$star', '$days', '>', '<', ' ', '='
        ], ['stars', 'days', '', '', '', ':'], $data['condition']);

        $temps = explode('&&', $condition);

        $data['stars'] = 0;
        $data['days'] = 0;
        foreach ($temps as $item) {
            list($key, $vls) = explode(':', $item);
            $data[$key] = $vls;
        }
        return true;
    }

    /**
     * @inheritdoc
     */
    public function getTransferOptions() {
        return [
            'table' => [
                'college_achieve' => 'a_achieve',
            ],
            'columns' => [
                'id',
                'name',
                'condition',
                'stars',
                'days',
                'begin_time' => function($data) {
                    if ($data['days']) {
                        return strtotime('2018-12-07 00:00:00');
                    }
                    return 0;
                },
                'end_time' => function($data) {
                    if ($data['days']) {
                        return strtotime('2019-01-15 23:59:59');
                    }
                    return 0;
                },
                'prop_id',
                'prop_num',
                'img_default' => function($data) {
                    return '/' . ltrim($data['img_default'], '/');
                },
                'img_unfinished' => function($data) {
                    return '/' . ltrim($data['img_unfinished'], '/');
                },
                'img_finished' => function($data) {
                    return '/' . ltrim($data['img_finished'], '/');
                },
                'user_count',
                'type',
                'describe',
                'sort',
                'status' => function() {
                    return 10;
                },
                'created_at' => function($data) {
                    if ($data['created_at']) {
                        return $data['created_at'];
                    }
                    return $data['updated_at'];
                },
                'updated_at',
                'deleted_at',
            ],
        ];
    }

}
